package com.ruoyi.mq.service.serviceImpl.Processor;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.mq.service.MessageProcessor;
import com.ruoyi.system.domain.GwRouter;
import com.ruoyi.system.service.IGwRouterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 处理路由信息类
 */
@Component
public class RoutingInfoProcessor implements MessageProcessor {
    @Autowired
    private IGwRouterService gwRouterService;
    public void processMessage(JSONObject message) {
        // 处理公司信息
        String tag = message.getString("tag");
        // 处理路由信息
        String data = message.getString("data");
        // 创建 ObjectMapper 实例
        ObjectMapper objectMapper = new ObjectMapper();

        switch (tag){
            case "add":
                // 转换为自定义对象
                try {
                    String niot = message.getString("niot");
                    GwRouter gwRouter = objectMapper.readValue(data, GwRouter.class);
                    gwRouter.setNodeIdentifier(niot);
                    gwRouterService.insertGwRouter(gwRouter);

                } catch (JsonProcessingException e) {
                    throw new RuntimeException(e);
                }

                break;
            case "update":
                // 转换为自定义对象
                try {
                    GwRouter gwRouter = objectMapper.readValue(data, GwRouter.class);
                    gwRouterService.updateGwRouter(gwRouter);

                } catch (JsonProcessingException e) {
                    throw new RuntimeException(e);
                }

                break;
            case "delete":

                //批量删除
                String[] parts = data.replaceAll("[\\[\\]]", "").split(",");

                for (String part : parts) {
                    //删除路由信息
                    gwRouterService.deleteGwRouterById(Long.valueOf(part));
                }
                break;

            default:
                break;

        }



        // 转换为自定义对象



    }

}
